﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DA1
{
    public partial class CT_ThanhPham : Form
    {
        public CT_ThanhPham()
        {
            InitializeComponent();
        }

        private void CT_ThanhPham_Load(object sender, EventArgs e)
        {
            try
            {
                ThanhPham_BUS bus = new ThanhPham_BUS();
                Search_BUS bus_xe = new Search_BUS();
                cmb_LoaiTP_Xem.DataSource = bus_xe.GetCmdXe();
                cmb_LoaiTP_Xem.DisplayMember = "TenLoai1";
                cmb_LoaiTP_Xem.ValueMember = "MaLoai";
            }catch(Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi: " + ex.ToString();
                Lbl_ThongBao.Visible = true;
            }
        }

        private void btnNhapCT_TP_Click(object sender, EventArgs e)
        {
            try
            {
                ThanhPham_BUS bus = new ThanhPham_BUS();
                ThanhPham_DTO dto = new ThanhPham_DTO();
                dto.MaTP = int.Parse(txtMaTP.Text.ToString());
                dto.NgayTao = DateTime.Parse(DTP_NgayTaoTP.Text.ToString());
                dto.SoLuong = int.Parse(txt_SoLuong_CT.Text.ToString());
                bus.proc_Ins_SoLuongThanhPhamMoiNgay(dto);
                Lbl_ThongBao.Text ="Nhập thành công";
                Lbl_ThongBao.Visible = true;
                //Load lai man hinh
                DGV_DanhSachTP.DataSource = bus.GetDanhSachTPTheoXe(int.Parse(cmb_LoaiTP_Xem.SelectedValue.ToString()));
            }catch(Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi: " + ex.ToString();
                Lbl_ThongBao.Visible = true;
            }
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            try
            {
                Lbl_ThongBao.Visible = false;
                ThanhPham_BUS bus = new ThanhPham_BUS();
                DGV_DanhSachTP.DataSource = bus.GetDanhSachTPTheoXe(int.Parse(cmb_LoaiTP_Xem.SelectedValue.ToString()));
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi: " + ex.ToString();
                Lbl_ThongBao.Visible = true;
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DGV_DanhSachTP_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Lbl_ThongBao.Visible = false;
                int i = DGV_DanhSachTP.CurrentRow.Index;
                if ((e.RowIndex == i) && (e.ColumnIndex == -1))
                {
                    Lbl_ThongBao.Visible = false;
                    txtMaTP.Text = DGV_DanhSachTP.Rows[i].Cells["MaTP"].Value.ToString();
                    txtTenTP.Text = DGV_DanhSachTP.Rows[i].Cells["TenTP"].Value.ToString();
                }
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi: " + ex.ToString();
                Lbl_ThongBao.Visible = true;
            }
        }
    }
}
